package com.it.travel.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.travel.common.ConstEnum;
import com.it.travel.dao.PermissionMapper;
import com.it.travel.pojo.Permission;
import com.it.travel.service.admin.AdminPermissionService;
import com.it.travel.vo.Params.PageParams;
import com.it.travel.vo.Params.admin.AdminPermissionAddFirstParams;
import com.it.travel.vo.Params.admin.AdminPermissionAddParams;
import com.it.travel.vo.Params.admin.AdminPermissionUpdateParams;
import com.it.travel.vo.Result;
import com.it.travel.vo.admin.AdminPermissionVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminPermissionServiceImpl implements AdminPermissionService {
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Result<?> getPermissionInfoPage(PageParams pageParams) {
        LambdaQueryWrapper<Permission> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(pageParams.getKeywords())) {
            wrapper.like(Permission::getPermissionName, pageParams.getKeywords());
        }
        HashMap<String, Object> map = new HashMap<>();
        Page<Permission> page = permissionMapper.selectPage(new Page<Permission>(pageParams.getIndex(), pageParams.getSize()), wrapper);
        List<Permission> records = page.getRecords();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        List<AdminPermissionVO> list = new ArrayList<>();
        for (Permission record : records) {
            AdminPermissionVO adminPermissionVO = new AdminPermissionVO();
            BeanUtil.copyProperties(record, adminPermissionVO);
            String format = simpleDateFormat.format(new Date(record.getCreateTime()));
            adminPermissionVO.setFormatTime(format);
            list.add(adminPermissionVO);
        }
        map.put("total", page.getTotal());
        map.put("permissionList", list);
        return Result.success(map, "获取数据成功");


    }

    @Override
    public Result<?> delete(Integer id) {
        Permission permission = permissionMapper.selectById(id);
        if (permission.getParentId() != ConstEnum.FIRST_PARENTID.getNum()) {  // 不是一级权限的话
            int i = permissionMapper.deleteById(id);
            if (i > 0) {
                return Result.success(null, "删除成功");
            }
            return Result.error(null, "删除失败");
        }
//            一级权限的话要进行级联删除
        LambdaQueryWrapper<Permission> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Permission::getParentId, id);
        List<Permission> permissionList = permissionMapper.selectList(wrapper); // 查询出当前一级权限下的所有权限的id进行级联删除
        int count = 0;
        for (Permission permission1 : permissionList) { // 有可能就是当前一级分类没有对应其2级分类

            permissionMapper.deleteById(permission1.getId());
            if (permissionList.size() == count) {
                return Result.success(null, "删除成功");
            }
        }
        permissionMapper.deleteById(id);
        return Result.success(null, "删除成功");
    }

    @Override
    public Result<?> saveFirstPermission(AdminPermissionAddFirstParams adminPermissionAddFirstParams) {
        Permission permission = new Permission();
        BeanUtil.copyProperties(adminPermissionAddFirstParams, permission);
        permission.setCreateTime(System.currentTimeMillis());
        permission.setUpdateTime(System.currentTimeMillis());
        permission.setParentId(ConstEnum.FIRST_PARENTID.getNum());
        int i = permissionMapper.insert(permission);
        if (i > 0) {
            return Result.success(null, "新增成功");
        }
        return Result.error(null, "新增失败");

    }

    @Override
    public Result<?> update(AdminPermissionUpdateParams adminPermissionUpdateParams) {
        Permission permission = new Permission();
        BeanUtil.copyProperties(adminPermissionUpdateParams, permission);
        permission.setUpdateTime(System.currentTimeMillis());
        int i = permissionMapper.updateById(permission);
        if (i > 0) {
            return Result.success(null, "修改成功");
        }
        return Result.error(null, "修改失败");
    }

    @Override
    public Result<?> findFirstName() {
        LambdaQueryWrapper<Permission> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Permission::getParentId, ConstEnum.FIRST_PARENTID.getNum());
        List<Permission> permissions = permissionMapper.selectList(wrapper);
        return Result.success(permissions, "获取数据成功");
    }

    @Override
    public Result<?> save(AdminPermissionAddParams adminPermissionAddParams) {
        if (adminPermissionAddParams.getParentId() == null) {
            return Result.error(null, "先请添加一级权限"); // 这里是为了防止前端没有选择一级权限
        }
        ;
        Permission permission = new Permission();
        BeanUtil.copyProperties(adminPermissionAddParams, permission);
        permission.setCreateTime(System.currentTimeMillis());
        permission.setUpdateTime(System.currentTimeMillis());
        permission.setParentId(adminPermissionAddParams.getParentId()); // 给当前添加的权限添加父级id
        int i = permissionMapper.insert(permission);
        if (i > 0) {
            return Result.success(null, "新增成功");
        }
        return Result.error(null, "新增失败");
    }

//    @Override
//    public Result<?> save(AdminPermissionAddParams adminPermissionAddParams) {
//
//
//
//    }
}
